package com.whnk.pm.service.impl;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.support.Convert;
import com.whnk.pm.domain.CtrInfo;
import com.whnk.pm.domain.CtrWlInfo;
import com.whnk.pm.domain.ImpSignInfo;
import com.whnk.pm.domain.IncomeDetail;
import com.whnk.pm.domain.IncomeInfo;
import com.whnk.pm.domain.ProInfo;
import com.whnk.pm.domain.ProMem;
import com.whnk.pm.mapper.CtrInfoMapper;
import com.whnk.pm.mapper.CtrWlInfoMapper;
import com.whnk.pm.mapper.ImpSignInfoMapper;
import com.whnk.pm.mapper.IncomeDetailMapper;
import com.whnk.pm.mapper.IncomeInfoMapper;
import com.whnk.pm.mapper.ProInfoMapper;
import com.whnk.pm.mapper.ProMemMapper;
import com.whnk.pm.service.IIncomeInfoService;
import com.whnk.pm.util.ValidateException;

/**
 * 确认单 服务层实现
 * 
 * @author whwhnk
 * @date 2019-07-24
 */
@Service
public class IncomeInfoServiceImpl implements IIncomeInfoService 
{
	@Autowired
	private IncomeInfoMapper incomeInfoMapper;

	@Autowired
	private IncomeDetailMapper incomeDetailMapper;

	@Autowired
	private ProInfoMapper proInfoMapper;

	@Autowired
	private CtrInfoMapper ctrInfoMapper;

	@Autowired
	private ProMemMapper proMemMapper;

	@Autowired
	private ImpSignInfoMapper impSignInfoMapper;

	@Autowired
	private CtrWlInfoMapper ctrWlInfoMapper;
	
	/**
     * 查询确认单信息
     * 
     * @param incId 确认单ID
     * @return 确认单信息
     */
    @Override
	public IncomeInfo selectIncomeInfoById(Integer incId)
	{
	    return incomeInfoMapper.selectIncomeInfoById(incId);
	}
	
	/**
     * 查询确认单列表
     * 
     * @param incomeInfo 确认单信息
     * @return 确认单集合
     */
	@Override
	public List<IncomeInfo> selectIncomeInfoList(IncomeInfo incomeInfo)
	{
	    return incomeInfoMapper.selectIncomeInfoList(incomeInfo);
	}
	
    /**
     * 新增确认单
     * 
     * @param incomeInfo 确认单信息
     * @return 结果
     */
	@Override
	public int insertIncomeInfo(IncomeInfo incomeInfo)
	{
	    return incomeInfoMapper.insertIncomeInfo(incomeInfo);
	}
	
	/**
     * 修改确认单
     * 
     * @param incomeInfo 确认单信息
     * @return 结果
     */
	@Override
	public int updateIncomeInfo(IncomeInfo incomeInfo)
	{
	    return incomeInfoMapper.updateIncomeInfo(incomeInfo);
	}

	/**
     * 删除确认单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteIncomeInfoByIds(String ids)
	{
		String[] idsArr = Convert.toStrArray(ids);
		
		StringBuilder idsStr = new StringBuilder("");
		for(String id : idsArr) {
			idsStr.append(id).append(";");
		}
		
		// 删除确认单明细
		incomeDetailMapper.deleteIncomeDetailByIncIds(idsArr);
		
		return incomeInfoMapper.deleteIncomeInfoByIds(Convert.toStrArray(ids));
	}

	@Override
	public void generateIncomeDetail(List<String> listId) {
		/**
		 * 根据listId 查询 pm_income_info
		 * 得到pro_id 项目id 查询pm_pro_info 得到项目具体信息
		 * pm_pro_info.pro_sn 行里的项目号   pm_pro_info.pro_sn.pro_self_sn 自己所在公司的项目号
		 * 			pm_pro_info.ctr_id  查询pm_ctr_info 得到项目具体信息
		 *
		 * 根据pro_id 查询pm_pro_mem 得到项目在场人员信息
		 *
		 * 	pm_income_info限定的时间查询pm_imp_sign_info 表查询打考勤的人 考勤天数
		 */
		List<IncomeInfo> incomeInfoList =  incomeInfoMapper.selectIncomeInfoByIds(listId);
		
		for (IncomeInfo incomeInfo : incomeInfoList) {
			
			Integer proId = incomeInfo.getProId();
			// 项目信息
			ProInfo proInfo = proInfoMapper.selectProInfoById(proId);
			
			// 项目人员
			ProMem selectProMem = new ProMem();
			selectProMem.setProId(proId);
			List<ProMem> proMemList = proMemMapper.selectProMemList(selectProMem);
			
			// 查询考勤人员
			DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String projectStartDate = simpleDateFormat.format(incomeInfo.getIncStdt());
			String projectEndDate = simpleDateFormat.format(incomeInfo.getIncEddt());
			List<ImpSignInfo> impSignInfoList = impSignInfoMapper.getSumBySignDateAndPro(projectStartDate, projectEndDate, proInfo.getProSn());
			
			// 删除当前确认单下的明细数据
			incomeDetailMapper.delIncomeDetailByIncId(incomeInfo.getIncId());
			
			// 统计 人员的打卡天数
			for (ImpSignInfo signInfo: impSignInfoList) {
				
				String empId = signInfo.getEmpId();
				String empNm = signInfo.getEmpNm();
				String compNm = signInfo.getEmpComp();
				
				// 考勤天数
				Double signDays = signInfo.getSignPd();
				
				// 人员级别
				String wlLevel = proMemList.stream().filter(k -> StringUtils.equalsIgnoreCase(empId, k.getEmpId())).findFirst().orElseGet(() -> {
					ProMem proMem = new ProMem();
					proMem.setWlLevel("0");
					return proMem;
				}).getWlLevel();
				
				// 根据项目ID查询订单信息
				CtrInfo ctrInfo = ctrInfoMapper.selectCtrListByProId(proId);
				
				if (null == ctrInfo || null == ctrInfo.getCtrId()) 
					continue;
				
				// 查询订单明细
				CtrWlInfo selectCtrWlInfo = new CtrWlInfo();
				selectCtrWlInfo.setCtrId(ctrInfo.getCtrId());
				List<CtrWlInfo> ctrWlInfos = ctrWlInfoMapper.selectCtrWlInfoList(selectCtrWlInfo);
				
				if (null == ctrWlInfos) 
					throw new ValidateException("请添加相应订单明细信息，以便查询人员级别");
				
				// 对应级别的单价
				Double wlUnp = ctrWlInfos.stream().filter(k -> StringUtils.equalsIgnoreCase(wlLevel, k.getWlLevel())).findFirst().orElseGet(() -> {
					CtrWlInfo ctrWlInfo = new CtrWlInfo();
					ctrWlInfo.setWlUnp(0d);
					return ctrWlInfo;
				}).getWlUnp();
				
				// 合计金额
				Double wlTotal = BigDecimal.valueOf(wlUnp).multiply(BigDecimal.valueOf(signDays)).doubleValue();

				// 插入新的明细数据
				IncomeDetail incomeDetail = new IncomeDetail();
				incomeDetail.setProSelfSn(proInfo.getProSelfSn());
				incomeDetail.setProName(proInfo.getProName());
				incomeDetail.setEmpId(empId);
				incomeDetail.setEmpNm(empNm);
				incomeDetail.setWlLevel(wlLevel);
				incomeDetail.setWlUnp(wlUnp);
				incomeDetail.setSignDays(signDays);
				incomeDetail.setWlTotal(wlTotal);
				incomeDetail.setIncId(incomeInfo.getIncId());
				incomeDetail.setCompNm(compNm);
				incomeDetailMapper.insertIncomeDetail(incomeDetail);
			}
		}
	}
}
